/*
 * @(#)NewsTableModel.java 0.0.1-SNAPSHOT
 *
 * Copyright Group B
 */
package grupb.upf.gui;

import grupb.upf.XMLObjects.New;
import java.util.LinkedList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

/**
 * <p>This is the model of the table of news.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public final class NewsTableModel implements javax.swing.table.TableModel {

    /**
     * List of subscribers. It is necessary to implement the class TableModel.
     */
    private LinkedList subscribers = new LinkedList();
    /**
     * List of listeners. It is necessary to implement the class TableModel.
     */
    private LinkedList listeners = new LinkedList();
    /**
     * The information, in this case a list of DescriptionContents
     */
    private LinkedList data = new LinkedList();

    /**
     * Method to get the number of rows.
     * @return the number of rows
     */
    @Override
    public int getRowCount() {
        return data.size();
    }

    /**
     * Method to get the number of columns.
     * @return The number of columns
     */
    @Override
    public int getColumnCount() {
        return 4;
    }

    /**
     * Method to get the name of a column.
     * @param columnIndex the index of the column
     * @return the name
     */
    @Override
    public String getColumnName(final int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "ID";
            case 1:
                return "Title";
            case 2:
                return "Date";
            case 3:
                return "Tags";
            default:
                return null;
        }
    }

    /**
     * Method to get the class of a column.
     * @param columnIndex the index of the column
     * @return the class of a column
     */
    @Override
    public Class<?> getColumnClass(final int columnIndex) {
        if (columnIndex == 0) {
            return Integer.class;
        } else {
            return String.class;
        }
    }

    /**
     * Method to know if a cell is editable.
     * @param rowIndex the index of the row
     * @param columnIndex the index of the column
     * @return always false
     */
    @Override
    public boolean isCellEditable(final int rowIndex, final int columnIndex) {
        return false;
    }

    /**
     * Method to get a value in a concrete row, the Column doesn't matter.
     * @param rowIndex the index of the row
     * @param columnIndex it doesn't matter
     * @return the value of the object DescriptionContent.
     */
    @Override
    public Object getValueAt(final int rowIndex, final int columnIndex) {
        if (data != null) {
            final New nw = (New) data.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    return nw.getID();
                case 1:
                    return nw.getTitle();
                case 2:
                    final String date = nw.getDate();
                    final String newDate = date.substring(0, 4) + "/" +
                            date.substring(4, 6) + "/" + date.substring(6, 8);
                    return newDate;
                case 3:
                    return nw.getTags().getTagsAsString();
                default:
                    return null;
            }
        }
        return null;
    }

    /**
     * Method to delete a new.
     * @param row
     */
    public void deleteNew(final int row) {
        data.remove(row);
        final TableModelEvent event = new TableModelEvent(this, row, row,
                TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE);
        notice(event);
    }

    /**
     * Method to add a new.
     * @param nw the new to be added
     */
    public void addNew(final New nw) {
        data.add(nw);
        final TableModelEvent event = new TableModelEvent(this,
                this.getRowCount() - 1, this.getRowCount() - 1,
                TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
        notice(event);
    }

    /**
     * Method to get a concrete new.
     * @param row the row
     * @return the New
     */
    public New getNew(final int row) {
        return (New) data.get(row);
    }

    /**
     * Method to set a value at a concrete position.
     * @param aValue the value
     * @param rowIndex the index of the row
     * @param columnIndex the index of the column
     */
    @Override
    public void setValueAt(final Object aValue, final int rowIndex,
            final int columnIndex) {
    }

    /**
     * Method to add a listener.
     * @param l the listener to be added
     */
    @Override
    public void addTableModelListener(final TableModelListener l) {
        subscribers.add(l);
    }

    /**
     * Method to remove a listener or subscriber.
     * @param l the listener
     */
    @Override
    public void removeTableModelListener(final TableModelListener l) {
        subscribers.remove(l);
    }

    /**
     * Method to notice to subscribers a change.
     * @param event the event
     */
    private void notice(final TableModelEvent event) {
        int i;
        for (i = 0; i < listeners.size(); i++) {
            ((TableModelListener) listeners.get(i)).tableChanged(event);
        }
    }
}